package com.jblog.controller.front.action.blog;

import static com.jblog.Constants.System.OK;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jblog.ErrorCode;
import com.jblog.common.util.ValidateUtil;
import com.jblog.controller.front.action.BaseAct;
import com.jblog.controller.web.SiteUtils;
import com.jblog.core.model.account.Account;
import com.jblog.core.model.blog.Comment;
import com.jblog.core.model.common.Status;

@Controller
@RequestMapping("/comment")
public class CommentAct extends BaseAct {
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody String add(HttpServletRequest request, Comment comment) {
        String msg = addValidation(request, comment);
        if (!OK.equals(msg)) {
            return msg;
        }
        
        comment.setEmail(comment.getEmail().toLowerCase());
        comment.setIp(request.getRemoteAddr());
        comment.setStatus(Status.NORMAL);

        Account account = SiteUtils.getAccount(request);
        if (null != account) {
            comment.setPhoto(account.getPhoto());
        }
        
        iblog.saveComment(comment);
        Long id = comment.getId();
        if (id < 0) {
            return ErrorCode.Common.DB_ERROR;
        }
        return OK;
    }

    private String addValidation(HttpServletRequest request, Comment comment) {
        String result;
        //validate of commentator
        result = ValidateUtil.helperRegex(comment.getCommentator(), "\\w{2,15}",
                ErrorCode.Blog.Comment.NAME_ERROR);
        if (!OK.equals(result)) return result;
        
        //validate of email
        result = ValidateUtil.isEmail(comment.getEmail());
        if (!OK.equals(result)) {
            return result;
        }
      
        //validate of url    
        if (StringUtils.isNotBlank(comment.getLink())) {
            result = ValidateUtil.isUrl(comment.getLink());
            if (!OK.equals(result)) {
                return result;
            }
        }
        
        //validate of comment
        result = ValidateUtil.helperLength(comment.getComment(), 5, true,
                ErrorCode.Blog.Comment.COMMENT_ERROR);
        if (!OK.equals(result)) return result;
        
        //validate of captcha
        String captcha = ValidateUtil.captcha(request, comment.getCaptcha());
        if (!OK.equals(captcha)) {
            return captcha;
        }

        return OK;
    }
    
    @RequestMapping(value = "/digg", method = RequestMethod.POST)
    public @ResponseBody boolean digg(HttpServletRequest request, Long id, boolean type) {
        return id > 0;
    }
    
}